<template>
  <div class='addProject-container'>
    <el-row>
      <el-col :span="24">
        <div class="project-title">编辑项目</div>
      </el-col>
      <!-- 表单部分 -->
      <el-col class="pro-form">
        <el-form :model="ruleForm"
                 status-icon
                 :rules="rules"
                 ref="ruleForm"
                 label-width="120px"
                 class="demo-ruleForm">
          <el-form-item label="项目名称"
                        prop="pro_name">
            <el-input class="normal-input"
                      v-model="ruleForm.pro_name"></el-input>

          </el-form-item>
          <el-form-item label="项目简称"
                        prop="pro_nickName">
            <el-input class="normal-input"
                      v-model="ruleForm.pro_nickName"></el-input>
            <el-tooltip class="item"
                        effect="dark"
                        content="项目简称，方便手机显示项目名称"
                        placement="right">
              <el-button icon="el-icon-question"
                         type="text"></el-button>
            </el-tooltip>
          </el-form-item>
          <el-form-item label="业务类型"
                        prop="business_type">
            <el-select v-model="ruleForm.business_type"
                       class="normal-input"
                       placeholder="选择业务类型">
              <el-option v-for="item in typeList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="外部系统编码"
                        prop="system_num">
            <el-input class="normal-input"
                      v-model="ruleForm.system_num"></el-input>
          </el-form-item>
          <el-form-item label="项目状态"
                        prop="pro_state">
            <el-select v-model="ruleForm.pro_state"
                       class="normal-input"
                       placeholder="项目状态">
              <el-option v-for="item in projectList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="项目许可编号"
                        prop="pro_num">
            <el-input class="normal-input"
                      v-model="ruleForm.pro_num"></el-input>
          </el-form-item>
          <el-form-item label="专业类型"
                        prop="profess_type">
            <el-select v-model="ruleForm.profess_type"
                       class="normal-input"
                       placeholder="专业类型">
              <el-option v-for="item in professList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
            <el-tooltip class="item"
                        effect="dark"
                        content="完成项目体系配置后进行操作"
                        placement="right">
              <el-button icon="el-icon-question"
                         type="text"></el-button>
            </el-tooltip>
          </el-form-item>
          <el-form-item label="建筑类型"
                        prop="build_type">
            <el-select v-model="ruleForm.build_type"
                       class="normal-input"
                       placeholder="选择建筑类型">
              <el-option v-for="item in buildList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="合同总额(万元):"
                        prop="deal_total">
            <el-input class="normal-input"
                      v-model="ruleForm.deal_total"></el-input>
          </el-form-item>
          <el-form-item label="装配率(%):"
                        prop="deal_total">
            <el-input class="normal-input"
                      v-model="ruleForm.deal_total"></el-input>
          </el-form-item>
          <el-form-item label="签订时间:"
                        prop="deal_total">
            <el-input class="normal-input"
                      v-model="ruleForm.deal_total"></el-input>
          </el-form-item>
          <el-form-item label="技术应用:"
                        prop="deal_total">
            <el-input class="normal-input"
                      type="textarea"
                      v-model="ruleForm.deal_total"></el-input>
          </el-form-item>
          <el-form-item label="设计单位"
                        prop="build_unit">
            <div class="build-unit"
                 v-for="(item,index) in buildUnitArr"
                 :key="index">
              <el-select v-model="item.build_unit"
                         class="normal-input"
                         placeholder="选择设计单位">
                <el-option v-for="sonItem in professList"
                           :key="sonItem.value"
                           :label="sonItem.label"
                           :value="sonItem.value">
                </el-option>
              </el-select>
              <el-button type="primary"
                         icon="el-icon-plus"
                         @click="addUnit"
                         v-if="index==0?true:false">增加</el-button>
              <el-button v-else
                         icon="el-icon-minus"
                         type="info"
                         @click="deleteUnit(index)"></el-button>
            </div>
          </el-form-item>
          <el-form-item label="施工单位"
                        prop="build_unit">
            <div class="build-unit"
                 v-for="(item,index) in buildUnitArr"
                 :key="index">
              <el-select v-model="item.build_unit"
                         class="normal-input"
                         placeholder="选择施工单位">
                <el-option v-for="sonItem in professList"
                           :key="sonItem.value"
                           :label="sonItem.label"
                           :value="sonItem.value">
                </el-option>
              </el-select>
              <el-button type="primary"
                         icon="el-icon-plus"
                         @click="addUnit"
                         v-if="index==0?true:false">增加</el-button>
              <el-button v-else
                         icon="el-icon-minus"
                         type="info"
                         @click="deleteUnit(index)"></el-button>
            </div>
          </el-form-item>
          <el-form-item label="构件加工厂"
                        prop="build_unit">
            <div class="build-unit"
                 v-for="(item,index) in buildUnitArr"
                 :key="index">
              <el-select v-model="item.build_unit"
                         class="normal-input"
                         placeholder="选择构件加工厂">
                <el-option v-for="sonItem in professList"
                           :key="sonItem.value"
                           :label="sonItem.label"
                           :value="sonItem.value">
                </el-option>
              </el-select>
              <el-button type="primary"
                         icon="el-icon-plus"
                         @click="addUnit"
                         v-if="index==0?true:false">增加</el-button>
              <el-button v-else
                         icon="el-icon-minus"
                         type="info"
                         @click="deleteUnit(index)"></el-button>
            </div>
          </el-form-item>
          <el-form-item label="项目地址"
                        prop="pro_manager">
            <el-select v-model="ruleForm.pro_manager"
                       class="area-input"
                       placeholder="省">
              <el-option v-for="item in buildList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
            <el-select v-model="ruleForm.pro_manager"
                       class="area-input"
                       placeholder="市">
              <el-option v-for="item in buildList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
            <el-select v-model="ruleForm.pro_manager"
                       class="area-input"
                       placeholder="区">
              <el-option v-for="item in buildList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="项目经理"
                        prop="pro_manager">
            <el-select v-model="ruleForm.pro_manager"
                       class="normal-input"
                       placeholder="选择项目经理">
              <el-option v-for="item in buildList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="质量负责人"
                        prop="quality_manager">
            <el-select v-model="ruleForm.quality_manager"
                       class="normal-input"
                       placeholder="选择质量负责人">
              <el-option v-for="item in buildList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="生产负责人1"
                        prop="production_manager">
            <el-select v-model="ruleForm.production_manager"
                       class="normal-input"
                       placeholder="选择生产负责人1">
              <el-option v-for="item in buildList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="生产负责人2"
                        prop="production_manager_two">
            <el-select v-model="ruleForm.production_manager_two"
                       class="normal-input"
                       placeholder="选择生产负责人2">
              <el-option v-for="item in buildList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="生产负责人3"
                        prop="production_manager_three">
            <el-select v-model="ruleForm.production_manager_three"
                       class="normal-input"
                       placeholder="选择生产负责人3">
              <el-option v-for="item in buildList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="吊装负责人"
                        prop="charge_manager">
            <el-select v-model="ruleForm.charge_manager"
                       class="normal-input"
                       placeholder="选择吊装负责人">
              <el-option v-for="item in buildList"
                         :key="item.value"
                         :label="item.label"
                         :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="详细地址"
                        prop="detail_address">
            <el-input class="normal-input"
                      v-model="ruleForm.detail_address"></el-input>
          </el-form-item>
          <el-form-item label="建筑面积"
                        prop="build_area">
            <el-input class="normal-input"
                      v-model="ruleForm.build_area"></el-input>
          </el-form-item>
          <el-form-item label="项目效果图"
                        prop="detail_address">
            <el-upload class="avatar-uploader"
                       action="https://jsonplaceholder.typicode.com/posts/"
                       :show-file-list="false"
                       :on-success="handleAvatarSuccess"
                       :before-upload="beforeAvatarUpload">
              <img v-if="imageUrl"
                   :src="imageUrl"
                   class="avatar">
              <i v-else
                 class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>
          <el-form-item>
            <el-button type="primary">保存</el-button>
            <el-button @click="onBack">取消</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  data () {
    var checkProName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入项目名称'));
      } else if ((/^[\s　]|[ ]$/gi).test(value)) {
        callback(new Error('项目名称首尾不能含有空格！'));
      } else {
        callback();
      }
    };
    var checkProNickName = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入项目简称'));
      } else if ((/^[\s　]|[ ]$/gi).test(value)) {
        callback(new Error('项目简称首尾不能含有空格！'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        pro_name: "",
        pro_nickName: "",
        business_type: "",
        system_num: "",
        pro_state: "",
        pro_num: "",
        profess_type: "",
        build_type: "",
        deal_total: "",
        pro_manager: "",
        quality_manager: "",
        production_manager: "",
        production_manager_two: "",
        production_manager_three: "",
        charge_manager: "",
        detail_address: "",
        build_area: ""
      },
      rules: {
        pro_name: [
          { required: true, validator: checkProName, trigger: 'blur' },
        ],
        pro_nickName: [
          { required: true, validator: checkProNickName, trigger: 'blur' },
        ],
        business_type: [
          { required: true, message: '请选择业务类型', trigger: 'change' },
        ],
        pro_state: [
          { required: true, message: '请选择项目状态', trigger: 'change' },
        ],
        profess_type: [
          { required: true, message: '请选择专业类型', trigger: 'change' },
        ],
        build_type: [
          { required: true, message: '请选择建筑类型', trigger: 'change' },
        ],
        detail_address: [
          { required: true, message: '请输入详细地址', trigger: 'blur' },
        ]
      },
      typeList: [],
      projectList: [],
      professList: [
        {
          value: '1',
          label: "建设单位",
        }, {
          value: "2",
          label: "检测单位"
        }
      ],
      buildList: [],
      buildUnitArr: [
        {
          build_unit: ""
        }
      ],
      imageUrl: '',
    }
  },
  methods: {
    //返回触发后执行事件
    onBack () {
      this.$router.push({ path: '/projectList' })
    },
    //添加单位功能
    addUnit () {
      this.buildUnitArr.push({ build_unit: "" });
    },
    //删除单位功能
    deleteUnit (index) {
      this.buildUnitArr.splice(index, 1);
    },
    //图片上传功能
    handleAvatarSuccess (res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    //图片上传前的钩子
    beforeAvatarUpload (file) {

    }
  }
}
</script>

<style scoped lang='scss'>
.addProject-container {
  background: #fff;
  .project-title {
    padding: 20px;
    border-bottom: 1px solid #eff2f7;
    font-size: 16px;
    font-weight: 300;
  }
  .pro-form {
    padding: 20px 0px;
    .normal-input {
      width: 40%;
    }
    .build-unit {
      margin-bottom: 20px;
    }
    .area-input {
      width: 13%;
    }
    /deep/ .avatar-uploader .el-upload {
      border: 1px dashed #d9d9d9;
      border-radius: 6px;
      cursor: pointer;
      position: relative;
      overflow: hidden;
    }
    .avatar-uploader .el-upload:hover {
      border-color: #409eff;
    }
    .avatar-uploader-icon {
      font-size: 28px;
      color: #8c939d;
      width: 178px;
      height: 178px;
      line-height: 178px;
      text-align: center;
    }
    .avatar {
      width: 178px;
      height: 178px;
      display: block;
    }
  }
}
</style>
